﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Leuphana.GuidedTour.App.ViewModels;
using System.Device.Location;

namespace Leuphana.GuidedTour.App.Pages
{
    public partial class TourPage : PhoneApplicationPage
    {
        private TourViewModel _viewModel;

        private GeoCoordinateWatcher _watcher;

        public TourPage()
        {
            InitializeComponent();

            // Attach viewmodel
            DataContext = _viewModel = App.GetTour();

            if (App.UsePosition)
            {
                // Create geowatcher
                _watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);
                _watcher.PositionChanged += _watcher_PositionChanged;
                _watcher.StatusChanged += _watcher_StatusChanged;
            }
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            string param;
            if (NavigationContext.QueryString.TryGetValue("type", out param))
            {
                if (param == "highlights")
                {
                    ContentItems.SelectedIndex = 1;
                }
            }
        }

        void _watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
        {
            if (e.Status == GeoPositionStatus.Disabled)
            {
                MessageBox.Show("Positionsdienste sind deaktiviert. Bitte aktiviere die Positionsdienste in den Einstellungen.");
            }
            else if (e.Status == GeoPositionStatus.NoData)
            {
                MessageBox.Show("Position konnte nicht ermittelt werden.");
            }
        }

        void _watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            Dispatcher.BeginInvoke(() =>
            {
                LocationPin.Visibility = Visibility.Visible;
                LocationPin.Location = e.Position.Location;
                Map.SetView(e.Position.Location, 13);
                _watcher.Stop();
            });
        }

        private void CollectionViewSource_Filter_Highlights(object sender, System.Windows.Data.FilterEventArgs e)
        {
            var item = e.Item as HotSpotViewModel;
            if (item == null)// || !item.IsHighlight)
            {
                e.Accepted = false;
            }

            e.Accepted = true;
        }

        private void Map_Loaded(object sender, RoutedEventArgs e)
        {
            Map.SetView(new GeoCoordinate(53.476183, 10.038865), 12);
        }

        private void AppBar_Position_Click(object sender, EventArgs e)
        {
            if (_watcher != null)
            {
                _watcher.Start();
            }
            else
            {
                MessageBox.Show("Positionsdienste sind deaktiviert. Bitte aktiviere die Positionsdienste in den Einstellungen.");
            }
        }

        private void AppBar_Heuckenlock_Click(object sender, EventArgs e)
        {
            Map.SetView(new GeoCoordinate(53.476183, 10.038865), 12);
        }
    }
}